package com.malty.rocketmq.manager;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 事务消息生产者需要向broker同步本地事务的状态
 *
 * @author malty
 */
@Slf4j
@Component
@RocketMQTransactionListener(txProducerGroup = "tx_test")
public class TxProductListener implements RocketMQLocalTransactionListener {
    /**
     * @param msg 消息
     * @param arg
     * @return
     */
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
        RocketMQLocalTransactionState state = RocketMQLocalTransactionState.UNKNOWN;
        log.info("本地事务执行>>>>>> msg: {} 结果：{}", msg.getPayload(), state);
        return state;
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
        RocketMQLocalTransactionState state = RocketMQLocalTransactionState.COMMIT;
        log.info("检查本地事务状态>>>>>> msg: {}, 结果：{}", msg.getPayload(), state);
        return state;
    }
}
